<?php

class task extends page{
	
	private $searchBook = 1;
	private $updateBook = 2;
	
	private $statusCancel = 0;
	private $statusStart = 1;
	private $statusClose = 2;
	
	function create($term=null, $type=null){
		$uid = 0;
		$is_return = false;
		if(!$term){
			$term = trim($this->req['term']);
			$uid = $this->req['uid'];
			$is_return = true;
			$type = $this->searchBook;
		}
		
		if(!$type){
			$type = $this->searchBook;
		}
		
		$json = array();
		if(!$term){		// Term 验证
			if($is_return){
				$json['status'] = $this->f_status;
				$json['message'] = "无搜索关键字";
				G::toClient($json);				
			}else{
				return false;
			}
		}
		
		$check_sql = "select count(*) as c from task where uid = {$uid} and type='{$type}' and content='{$term}' and status=1";
		$check_rs = $this->db->query($check_sql)->fetch();
		if($check_rs['c']>0){	// 重复验证
			if($is_return){
				$json['status'] = $this->f_status;
				$json['message'] = "请勿重复创建";
				G::toClient($json);
			}else{
				return false;
			}
		}	
		
		$sql = "INSERT INTO task (content, uid, type, create_time, update_time) VALUES ('{$term}', '{$uid}', '{$type}', now(), now())";
		$rs = $this->db->exec($sql);
		$json['status'] = $this->s_status;
		if(!$rs){
			$json['status'] = $this->f_status;
		}
		if($is_return){
			G::toClient($json);
		}else{
			return true;
		}
	}
	
	function delete(){
		$uid = intval($this->req['uid']);
		$id = intval($this->req['id']);
		
		$json = array();
		if(!uid && !id){
			$json['status'] = $this->f_status;
			$json['message'] = '缺少必须字段';
			G::toClient($json);
		}
		
		$sql = "update task set status = '{$this->statusCancel}' where uid = '{$uid}' and id = '{$id}' limit 1";
		$rs = $this->db->exec($sql);
		
		$json['status'] = $this->s_status;
		if(!$rs){
			$json['status'] = $this->f_status;
		}
		G::toClient($json);
	}
	
	function taskList(){
		$uid = intval($this->req['uid']);
		
		$json = array();
		if(!$uid){
			$json['status'] = $this->f_status;
			$json['message'] = 'uid不能为空';
			G::toClient($json);
		}
		
		$sql = "SELECT id, content, uid, status FROM task WHERE uid = '{$uid}' and status != {$this->statusCancel}";
		$rs = $this->db->query($sql)->fetchAll();
		$json['status'] = $this->s_status;
		$json['list'] = $rs;
		G::toClient($json);
	}
	
	function addFeedback(){
		$device_id = $this->req['d'];
		$title = $this->req['title'];
		$content = $this->req['content'];
		$qq = $this->req['qq'];

		$json = array();
		if(!$device_id){
			$json['status'] = $this->f_status;
                        $json['message'] = 'Missing Device ID';
                        G::toClient($json);
		}
		
		$insert_sql = "INSERT INTO feedback (device_id, title, content, qq, create_time) VALUES ('{$device_id}','{$title}','{$content}','{$qq}',now())";
		$r = $this->db->exec($insert_sql);

		$json['status'] = $this->s_status;
		if(!$r){
			$json['status'] = $this->f_status;
		}
		G::toClient($json);
	}	
}
